package MicroGZM;

import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_SMOOTH;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_COORD_ARRAY;
import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
import static org.lwjgl.opengl.GL11.GL_VERTEX_ARRAY;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glDisableClientState;
import static org.lwjgl.opengl.GL11.glDrawArrays;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glEnableClientState;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glRotatef;
import static org.lwjgl.opengl.GL11.glShadeModel;
import static org.lwjgl.opengl.GL11.glTexCoordPointer;
import static org.lwjgl.opengl.GL11.glTranslatef;
import static org.lwjgl.opengl.GL11.glVertexPointer;
import static org.lwjgl.util.glu.GLU.gluPerspective;

import java.io.IOException;
import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

import MicroGZM.mesh.Mesh;

public class MicroGZMRenderer {
	
	private FloatBuffer vertexBuffer;
	private FloatBuffer textureCoordsBuffer;
	
	private int totalVertices;
	
	private float val = 0.0f;
	
	private Texture texture;
	
	public MicroGZMRenderer() {
		
		loadTextures();
		setupOpenGL();
	}

	private void loadTextures() {
		try {
			texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/textures/wood.png"));
		} catch (IOException e) {

			e.printStackTrace();
		}
		
	}

	private void setupOpenGL() {
		
		glEnable(GL_TEXTURE_2D);
		glShadeModel(GL_SMOOTH);
		
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective((float) 30, 640f / 480f, 0.001f, 1000f);
		glMatrixMode(GL_MODELVIEW);
	}
	
	public void update() {
		
		texture.bind();
		
		glLoadIdentity();
		glTranslatef(0, 0, -50);
		glRotatef(val, 1, 0, 0);
		glRotatef(val, 0, 1, 0);

		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		
		glEnableClientState(GL_VERTEX_ARRAY);
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);

		glVertexPointer(3,0,vertexBuffer);
		glTexCoordPointer(3,0,textureCoordsBuffer);
		
		glDrawArrays(GL_TRIANGLES, 0, totalVertices);

		glDisableClientState(GL_TEXTURE_COORD_ARRAY);
		glDisableClientState(GL_VERTEX_ARRAY);
		
		val+=1.1f;
	}
	
	public void addMesh(Mesh m)
	{
		totalVertices = m.getVertexArray().length;
		
		addVertexBuffer(m.getVertexArray());
		addTextureCoordsBuffer(m.getTextCoordsArray());
	}
	
	private void addVertexBuffer(float[] data) {

		vertexBuffer = BufferUtils.createFloatBuffer(totalVertices * 3);
		vertexBuffer.put(data);
		vertexBuffer.flip();
		
		updateBuffers();
	}
	
	private void addTextureCoordsBuffer(float[] data) {

		textureCoordsBuffer = BufferUtils.createFloatBuffer(data.length * 2);
		textureCoordsBuffer.put(data);
		textureCoordsBuffer.flip();
		
		updateBuffers();
	}

	private void updateBuffers() {
		
	}
}
